import RPi.GPIO as gpio
import time
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
time.sleep(1)
data=[]
def delay(i): #20*i usdelay
a=0
for j in range(i):
a+1
j=0


#start work
pin=15
#这里的pin代表接的第几个GPIO口
gpio.setup(pin,gpio.OUT)
#gpio.output(pin,gpio.HIGH)
#delay(10)
gpio.output(pin,gpio.LOW)
time.sleep(0.02)
gpio.output(pin,gpio.HIGH)
i=1
i=1
#wait to response
gpio.setup(pin,gpio.IN)




while gpio.input(pin)==1:
continue




while gpio.input(pin)==0:
continue


while gpio.input(pin)==1:
continue
#get data


while j<40:
k=0
while gpio.input(pin)==0:
continue

while gpio.input(pin)==1:
k+=1
if k>100:
gpio.setup(pin,gpio.OUT)
gpio.output(pin,gpio.LOW)
break
if k<3:
data.append(0)
else:
data.append(1)
j+=1

#print "Sensor is working"
#get temperature
#print data[0:40]
humidity_bit=data[0:16]
#humidity_point_bit=data[8:16]
temperature_bit=data[16:32]
#temperature_point_bit=data[24:32]
#check_bit=data[32:40]
chek1_bit=data[0:8]
chek2_bit=data[8:16]
chek3_bit=data[16:24]
chek4_bit=data[24:32]
chek5_bit=data[32:40]
chek1=0
chek2=0
chek3=0
chek4=0
chek5=0




humidity=0
#humidity_point=0
temperature=0








for i in range(16):
humidity+=humidity_bit[i]*2**(15-i)
# humidity_point+=humidity_point_bit[i]*2**(7-i)
temperature+=temperature_bit[i]*2**(15-i)
# temperature_point+=temperature_point_bit[i]*2**(7-i)
#for i in range(8):
# check+=check_bit[i]*2**(7-i)


for i in range(8):
chek1+=chek1_bit[i]*2**(7-i)
chek2+=chek2_bit[i]*2**(7-i)
chek3+=chek3_bit[i]*2**(7-i)
chek4+=chek4_bit[i]*2**(7-i)
chek5+=chek5_bit[i]*2**(7-i)
#print chek1,chek2,chek3,chek4,chek5


#tmp=humidity+temperature
if (chek5==chek1+chek2+chek3+chek4) and 0<humidity<1000 and 0<temperature<500:
temperature=float(temperature)/10
humidity=float(humidity)/10
print temperature,humidity,"1"
else:
print "0","0","0"
#print temperature,humidity,check